package com.pateo.retail.canal.handler.es;

import com.pateo.retail.canal.constant.CommonConstants;
import com.pateo.retail.canal.es.EsService;
import com.pateo.retail.canal.es.vo.EsAddReqVo;
import com.pateo.retail.canal.es.vo.EsUpsertReqVo;
import com.pateo.retail.canal.model.AllEsCustomerInfo;
import com.pateo.retail.canal.model.CustomerInfo;
import com.pateo.retail.canal.model.ParentCustomerJoinInfo;
import com.pateo.retail.canal.utils.CommonUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import top.javatool.canal.client.annotation.CanalTable;
import top.javatool.canal.client.handler.EntryHandler;

/**
 * @author gujiachun
 */
@Component
@CanalTable(value = "customer_info")
public class CustomerInfoHandler implements EntryHandler<CustomerInfo> {

    private Logger logger = LoggerFactory.getLogger(CustomerInfoHandler.class);

    @Autowired
    private EsService esService;

    @Override
    public void insert(CustomerInfo customerInfo) {
        logger.info("insert message  {}", customerInfo);
        if (StringUtils.isEmpty(customerInfo.getTenant_code())) {
            logger.info("数据异常tenantCode为null，不执行同步es同步逻辑-----{}", customerInfo);
            return;
        }

        ///////////////标识此文档为父文档////////////////
        ParentCustomerJoinInfo parentCustomerJoinInfo = new ParentCustomerJoinInfo();
        parentCustomerJoinInfo.setName(CommonConstants.CUSTOMER_JOIN_INFO_PARENT);
        customerInfo.setCustomer_join_info(parentCustomerJoinInfo);
        ///////////////标识此文档为父文档////////////////
        EsUpsertReqVo reqVo = new EsUpsertReqVo();
        reqVo.setData(customerInfo);
        reqVo.setIndex(CommonUtils.getEsIndex(customerInfo.getTenant_code()));
        reqVo.setId(customerInfo.getSid() + "");
        esService.upsert(reqVo);
    }

    @Override
    public void update(CustomerInfo before, CustomerInfo after) {
        logger.info("update before {} ", before);
        logger.info("update after {}", after);

        if (StringUtils.isEmpty(after.getTenant_code())) {
            logger.info("数据异常tenantCode为null，不执行同步es同步逻辑-----{}", after);
            return;
        }

        ParentCustomerJoinInfo parentCustomerJoinInfo = new ParentCustomerJoinInfo();
        parentCustomerJoinInfo.setName(CommonConstants.CUSTOMER_JOIN_INFO_PARENT);
        after.setCustomer_join_info(parentCustomerJoinInfo);

        EsUpsertReqVo reqVo = new EsUpsertReqVo();
        reqVo.setData(after);
        reqVo.setIndex(CommonUtils.getEsIndex(after.getTenant_code()));
        reqVo.setId(after.getSid() + "");
        esService.upsert(reqVo);
    }

    @Override
    public void delete(CustomerInfo customerInfo) {
        logger.info("delete  {}", customerInfo);

        if (StringUtils.isEmpty(customerInfo.getTenant_code())) {
            logger.info("数据异常tenantCode为null，不执行同步es同步逻辑-----{}", customerInfo);
            return;
        }

        esService.delete(CommonUtils.getEsIndex(customerInfo.getTenant_code()), customerInfo.getSid() + "");
    }
}
